//
//  BattleDestroyDetails.m
//  SpaceStorm
//
//  Created by  mmj on 13-8-8.
//
//
#import "Variable.h"


#import "BattleDestroyDetails.h"
#import "cocos2d.h"
#import "BubbleSprite.h"
#import "SlowBlood2Layer.h"

@implementation BattleDestroyDetailsInfoItem

+(id) itemWithIndex:(int)index
{
    return [[[self alloc] initWithIndex:index] autorelease];
}

-(id) initWithIndex:(int)index
{
    if((self = [super init]))
    {
        //名称
        labelName = [CCLabelTTF labelWithString:@"能量防御" fontName:@"Marker Felt" fontSize:10];
        
        //减免率
        labelreducDesc = [CCLabelTTF labelWithString:@"减免率" fontName:@"Marker Felt"  fontSize:10];
        
        //初始化自己总战斗力槽
        _blood = [SlowBlood2Layer bloodWithBG:@"battleOutLine.png" blood:@"battleGreenLine.png"];
        
        //百分比
        labelreducPercent = [CCLabelTTF labelWithString:@"100%" fontName:@"Marker Felt" fontSize:10];
        
        labelName.position = ccp(labelName.contentSize.width/2, labelName.contentSize.height/2);
        labelName.color = ccc3(255, 255, 255);
        
        [self addChild:labelName];
        
        
        _blood.position = ccp(92 + _blood.contentSize.width/2, 6);
        [self addChild:_blood];
        
        //减免率
        labelreducDesc.position = ccp(145+labelreducDesc.contentSize.width/2, labelreducDesc.contentSize.height/2);
        labelreducDesc.color = ccc3(255, 255, 255);
        [self addChild:labelreducDesc];
        
        //百分比
        labelreducPercent.position = ccp(182+labelreducPercent.contentSize.width/2, labelreducPercent.contentSize.height/2);
        labelreducPercent.color = ccc3(255, 255, 255);
        [self addChild:labelreducPercent];
        
        
    }
    return self;
}

/**
// 更新item 信息
 **/
-(void) updateDestroyDetailsInfo:(NSString *) labelName1 bloodPercent1:(int) bloodPercent1 reducPercent1:(int)reducPercent1 reducIndex:(int)reducIndex 
{

    [labelName setString:[NSString stringWithFormat:@"%@",labelName1]];  //是变量   还要传递过来的就是 @“实施”
    
    [labelreducPercent setString:[NSString stringWithFormat:@"%d%%",reducPercent1]];  //@"%d%%"
    [_blood setProgress:bloodPercent1];
    //减免率 百分比颜色
    switch (reducIndex)
    {
        case 0:
            labelreducPercent.color = ccc3(0, 255, 0);
            break;
        case 1:
            labelreducPercent.color = ccc3(255, 255, 255);
            break;
        case 2:
            labelreducPercent.color = ccc3(255, 0, 0);
            break;
        default:
            break;
    }
}


@end


@implementation BattleDestroyDetailsGoodsItem


+(id) itemWithIndex:(int)index
{
    return [[[self alloc] initWithIndex:index] autorelease];
}

-(id) initWithIndex:(int)index
{
    if( (self=[super init]))
    {
        CCSprite* goodsBlackBg;  //物品的背景
        goodsBlackBg = [CCSprite spriteWithSpriteFrameName:@"battleGridBg.png"];
        goodsBlackBg.position = ccp(goodsBlackBg.contentSize.width/2, goodsBlackBg.contentSize.height/2);
        [self addChild:goodsBlackBg];
        
        goodsBg = [CCSprite spriteWithSpriteFrameName:@"battleGoods.png"];
        goodsBg.position = ccp(goodsBg.contentSize.width/2+(goodsBlackBg.contentSize.width-goodsBg.contentSize.width)/2, goodsBg.contentSize.height/2+(goodsBlackBg.contentSize.height-goodsBg.contentSize.height)/2);
        [self addChild:goodsBg];
        
    }
    return self;
    
}

-(void) updateDestroyWinGoodsInfo:(int) goodsType
{
    //方法二  替换成功
    NSString *frameName = [NSString stringWithFormat:@"battleGridBg.png"];
   // NSString *frameName = [NSString stringWithFormat:@"%s.png","battleGridBg"];
//   NSString *frameName = [NSString stringWithFormat:@"%d.png",11];
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
    [goodsBg setDisplayFrame:frame];
    
    
}

@end





@implementation BattleDestroyDetails


@synthesize armNeed;
@synthesize powerNum;
@synthesize hurtList;
@synthesize protectList;


+(CCScene *) scene
{
    // 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	BattleDestroyDetails *layer = [BattleDestroyDetails node];
    
	[scene addChild: layer];
    
	return scene;
}


-(id) init
{
    if( (self=[super init]))
    {
        
        //缓冲图片文件
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"UIBattleSettle_480_320.plist"];
        
        // ask director the the window size
        size = [[CCDirector sharedDirector] winSize];
        
        
        //初始化背景
        [self initBackGround];
        
        //初始化标题头
        [self initHead];
        
        //菜单
        [self initMenu];
        
        //最上层的装饰
        [self initTopDecorate];
        
        // 信息
        [self initInfo:_hurtList protectList:_protectList goodsList:_goodsList];

        
        

        
        
        [self updateLabelInfo:30 powerNum:50];
    }
    
    return self;
}

/**
 初始化背景
 */
-(void) initBackGround
{
    CCSprite* backGround = [CCSprite spriteWithSpriteFrameName:@"storageBg.jpg"];
    backGround.position = ccp(size.width / 2 , size.height / 2);
    [self addChild:backGround];
    
    //九宫格资源
    BubbleSprite* jiuOutBg = [BubbleSprite spriteWithFrameName:@"storageJiuOutBg.png" size:CGSizeMake(408, 202) leftCap:16 topCap:16];
    jiuOutBg.position = ccp(35+jiuOutBg.contentSize.width/2, size.height - 47 - jiuOutBg.contentSize.height/2);
    [self addChild:jiuOutBg];
    
    //线条1 暗色
    CCSprite* drakLine1 = [CCSprite spriteWithSpriteFrameName:@"battleLineBg1.png"];
    float lineLen = 4.8;//398/81;
    drakLine1.scaleX = lineLen;
    drakLine1.position = ccp(size.width/2, size.height - 124-drakLine1.contentSize.height/2);
    [self addChild:drakLine1];
    
    //线条2 暗色
    CCSprite* drakLine2 = [CCSprite spriteWithSpriteFrameName:@"battleLineBg1.png"];
    drakLine2.scaleX = lineLen;
    drakLine2.position = ccp(size.width/2, size.height - 200-drakLine2.contentSize.height/2);
    [self addChild:drakLine2];
    
    //线条1
    CCSprite* lineBg1 = [CCSprite spriteWithSpriteFrameName:@"battleLineBg2.png"];
    lineBg1.position = ccp(size.width/2, size.height - 266 -lineBg1.contentSize.height/2);
    [self addChild:lineBg1];
    
    //线条2
    CCSprite* lineBg2 = [CCSprite spriteWithSpriteFrameName:@"battleLineBg2.png"];
    lineBg2.position = ccp(size.width/2, size.height - 305 - lineBg2.contentSize.height/2);
    [self addChild:lineBg2];
    
}

/**
 初始化头部
 */
-(void) initHead
{
    //标题背景
    CCSprite *headBg = [CCSprite spriteWithSpriteFrameName:@"titleBlackBg.png"];
    //标题歼灭胜利
    CCSprite *titlwineBg = [CCSprite spriteWithSpriteFrameName:@"battleTitle.png"];
    //明线条
    CCSprite *brigtLineBg = [CCSprite spriteWithSpriteFrameName:@"battleLightLine.png"];
    headBg.position = ccp((size.width/2) , (size.height - headBg.contentSize.height/2- 9));
    [self addChild:headBg];
    
    titlwineBg.position = ccp((size.width)/2 , (size.height - titlwineBg.contentSize.height/2)-12);
    [self addChild:titlwineBg];
    
    brigtLineBg.position = ccp(size.width/2, size.height - 39);
    [self addChild:brigtLineBg];
    
}

/**
 初始化菜单
 */
-(void) initMenu
{
    //战报
    CCSprite* powerNormal = [CCSprite spriteWithSpriteFrameName:@"battleBtnNormal.png"];
    CCSprite* powerClick = [CCSprite spriteWithSpriteFrameName:@"battleBtnClick.png"];
    
    //确认
    CCSprite* sureNormal = [CCSprite spriteWithSpriteFrameName:@"battleBtnNormal.png"];
    CCSprite* sureClick = [CCSprite spriteWithSpriteFrameName:@"battleBtnClick.png"];
    
    //左边箭头
    CCSprite* leftArrowNormal = [CCSprite spriteWithSpriteFrameName:@"battleArrowBg.png"];
    CCSprite* leftArrowClick = [CCSprite spriteWithSpriteFrameName:@"battleArrowBg.png"];
    
    //右边箭头
    CCSprite* rightArrowNormal = [CCSprite spriteWithSpriteFrameName:@"battleArrowBg.png"];
    CCSprite* rightArrowClick = [CCSprite spriteWithSpriteFrameName:@"battleArrowBg.png"];
    //game menu
    CCMenuItemSprite* power;  //战报
    CCMenuItemSprite* sure;     //确认
    CCMenuItemSprite* leftArrow;     //左边箭头
    CCMenuItemSprite* rightArrow;     //右边箭头
    
    //战报
    CCSprite* battleReport = [CCSprite spriteWithSpriteFrameName:@"battleReport.png"];
    //确认
    CCSprite* battleSure = [CCSprite spriteWithSpriteFrameName:@"battleSure.png"];
    
    //X轴 镜像反转
    [powerNormal setFlipX:YES];
    [powerClick setFlipX:YES];
    power = [CCMenuItemSprite itemFromNormalSprite:powerNormal selectedSprite:powerClick target:self selector:@selector(onPowerButton:)];
    power.position = ccp(power.contentSize.width / 2 +39 , size.height - power.contentSize.height/2 -268);
    
    sure = [CCMenuItemSprite itemFromNormalSprite:sureNormal selectedSprite:sureClick target:self selector:@selector(onSureButton:)];
    sure.position = ccp(size.width -  power.contentSize.width / 2 -39 , size.height - sure.contentSize.height/2 -268);
    
    leftArrow = [CCMenuItemSprite itemFromNormalSprite:leftArrowNormal selectedSprite:leftArrowClick target:self selector:@selector(onLeftArrowButton:)];
    leftArrow.position = ccp(130 + leftArrow.contentSize.width/2 , size.height - leftArrow.contentSize.height/2 -268);
    //X轴 镜像反转
    [rightArrowNormal setFlipX:YES];
    [rightArrowClick setFlipX:YES];
    rightArrow = [CCMenuItemSprite itemFromNormalSprite:rightArrowNormal selectedSprite:rightArrowClick target:self selector:@selector(onRightArrowButton:)];
    rightArrow.position = ccp(size.width - 130 - rightArrow.contentSize.width/2 , size.height - rightArrow.contentSize.height/2 -268);
    
    //菜单
    menu = [CCMenu menuWithItems:power, sure,leftArrow, rightArrow, nil];
    menu.position = CGPointZero;
    [self addChild:menu];
    
    //战报
    battleReport.position = ccp(battleReport.contentSize.width/2+66, size.height - 278 -battleReport.contentSize.height/2);
    [self addChild:battleReport];
    //确认
    battleSure.position = ccp(size.width - 100+ battleSure.contentSize.width/2  , size.height - 278 - battleSure.contentSize.height/2);
    [self addChild:battleSure];
    
}

/**
 初始化信息
 */
-(void) initInfo:(CCArray*) hurtList protectList:(CCArray*) protectList goodsList:(CCArray*)goodsList

{
    
    //伤害  防御信息
    BattleDestroyDetailsInfoItem * detailsInfoItem1;
    

    //伤害数据
    int i;
    int idMax;
    int idMin;
    int colorType1;
    //伤害数据
    for(i = 0; i<3; i++)
    {
        detailsInfoItem1 =  [BattleDestroyDetailsInfoItem itemWithIndex:i];
        detailsInfoItem1.position = ccp(51+detailsInfoItem1.contentSize.width/2, size.height - (84+18*i)-detailsInfoItem1.contentSize.height/2);
        [self addChild:detailsInfoItem1];
        //用于测试百分比 设定颜色
        idMax  = [self retMaxNum:30 yy:40 zz:50];
        idMin  = [self retMinNum:30 yy:40 zz:50];
        colorType1  = [self retColorType:idMin maxNum:idMax inputNum:30];

        [detailsInfoItem1 updateDestroyDetailsInfo:@"能量伤害" bloodPercent1:39 reducPercent1:90 reducIndex:colorType1];  //i 要改成colorType1 
    }
    //防御数据
    for(i = 0; i<3; i++)
    {
        detailsInfoItem1 =  [BattleDestroyDetailsInfoItem itemWithIndex:i];
        detailsInfoItem1.position = ccp(51+detailsInfoItem1.contentSize.width/2, size.height - (162+18*i)-detailsInfoItem1.contentSize.height/2);
        [self addChild:detailsInfoItem1];
        //用于测试百分比 设定颜色
        idMax  = [self retMaxNum:30 yy:40 zz:50];
        idMin  = [self retMinNum:30 yy:40 zz:50];
        colorType1  = [self retColorType:idMin maxNum:idMax inputNum:30];
        [detailsInfoItem1 updateDestroyDetailsInfo:@"能量防御" bloodPercent1:39 reducPercent1:90 reducIndex:i];
    }
    
    
    //战利品
    BattleDestroyDetailsGoodsItem * goodsItem1;
    for(i = 0;i<5;i++)
    {
        goodsItem1 = [BattleDestroyDetailsGoodsItem itemWithIndex:i];
        goodsItem1.position = ccp(147+39*i+goodsItem1.contentSize.width/2, 17-goodsItem1.contentSize.height/2);
        [self addChild:goodsItem1];
    }
    
  
    [self updateHurtDescInfo:@"敌人对动能伤害抗性较低，推荐使用动能武器进行打击" desc2:@"11"];
    [self updateProtectDescInfo:@"敌人伤害依赖动能武器，推荐提高动能抗性进行防御" desc2:@"11"];
    
    
}

/**
 三个数比较返回最大值
 */
-(int) retMaxNum:(int) xx yy:(int) yy zz:(int)zz
{
    int a;
    if(xx > yy && xx>zz) a = xx;
    if(yy>xx && yy> zz) a = yy;
    if(zz>xx && zz>yy) a = zz;
    return a;
}

/**
 三个数比较返回最小值
 */
-(int) retMinNum:(int) xx yy:(int) yy zz:(int)zz
{
    int a;
    if(xx < yy && xx<zz) a = xx;
    if(yy<xx && yy< zz) a = yy;
    if(zz<xx && zz<yy) a = zz;
    return a;
}

/**
 三个数比较返回最小值
 */
-(int) retColorType:(int) minNum maxNum:(int)maxNum inputNum:(int)inputNum
{
    int a;
    if(inputNum>= minNum && inputNum<maxNum) a = 0;
    if(inputNum>minNum && inputNum<maxNum) a = 1;
    if(inputNum > minNum && inputNum >= maxNum) a = 2;
    return a;
}

/**
 更新伤害描述
 */
-(void) updateHurtDescInfo:(NSString*) desc1 desc2:(NSString*) desc2
{

    [labelHurtDesc setString:[NSString stringWithFormat:@"%@",desc1]];
}

/**
 更新防御描述
 */
-(void) updateProtectDescInfo:(NSString*) desc1 desc2:(NSString*) desc2
{
    [labelProtectDesc setString:[NSString stringWithFormat:@"%@",desc1]];

}

-(void) updateLabelInfo:(int) armNeed powerNum:(int) powerNum
{
    
    [labelArmNeed setString:[NSString stringWithFormat:@"%d",armNeed]];
    [labelPower setString:[NSString stringWithFormat:@"%d",powerNum]];
    
    
    
    
}

/**
 初始化装饰
 */
-(void) initTopDecorate
{
    
    //战利品
    CCSprite* battleReard = [CCSprite spriteWithSpriteFrameName:@"battleReward.png"];
    battleReard.position = ccp(size.width/2, size.height - 257);
    [self addChild:battleReard];
    
    //军需
    CCSprite* armyNeed = [CCSprite spriteWithSpriteFrameName:@"battleArmyNeed.png"];
    armyNeed.position = ccp(299+armyNeed.contentSize.width/2, size.height - 259);
    [self addChild:armyNeed];
    //战功
    CCSprite* battlePower = [CCSprite spriteWithSpriteFrameName:@"battlePower.png"];
    battlePower.position = ccp(372+battlePower.contentSize.width/2, size.height - 259);
    [self addChild:battlePower];
    
    
    
    
    //军需
    labelArmNeed = [CCLabelTTF labelWithString:@"10000" fontName:@"Marker Felt" fontSize:10];
    //战功
    labelPower = [CCLabelTTF labelWithString:@"3000" fontName:@"Marker Felt" fontSize:10];
    
    //军需
    labelArmNeed.anchorPoint = CGPointMake(0, 0.5);
    labelArmNeed.position = ccp(size.width - 146, size.height - 259);
    labelArmNeed.color  = ccc3(0,255, 255);
    [self addChild:labelArmNeed];
    
    //战功
    labelPower.anchorPoint = CGPointMake(0, 0.5);
    labelPower.position = ccp(size.width - 72, size.height - 259);
    labelPower.color  = ccc3(0,255, 255);
    [self addChild:labelPower];
    
    //伤害
    CCLabelTTF* labelhurt = [CCLabelTTF labelWithString:@"伤害" fontName:@"Marker Felt" fontSize:14];
    labelhurt.position = ccp(52+labelhurt.contentSize.width/2, size.height - 54-labelhurt.contentSize.height/2);
    labelhurt.color = ccc3(255, 255, 153);
    [self addChild:labelhurt];
    
    //防御
    CCLabelTTF* labelProtect = [CCLabelTTF labelWithString:@"防御" fontName:@"Marker Felt" fontSize:14];
    labelProtect.position = ccp(52+labelProtect.contentSize.width/2,size.height - 131-labelProtect.contentSize.height/2);
    labelProtect.color = ccc3(255, 255, 153);
    [self addChild:labelProtect];
    
    //伤害 描述
    labelHurtDesc = [CCLabelTTF labelWithString:@"伤害 描述" fontName:@"Marker Felt" fontSize:10];

    labelHurtDesc.anchorPoint = CGPointMake(0, 0);
    labelHurtDesc.position = ccp(size.width - 206, size.height - 84);
    labelHurtDesc.color = ccc3(255, 255, 255);
    [self addChild:labelHurtDesc];
    
    //防御 描述
    labelProtectDesc = [CCLabelTTF labelWithString:@"防御 描述" fontName:@"Marker Felt" fontSize:10];
    labelProtectDesc.anchorPoint = CGPointMake(0, 0);
    labelProtectDesc.position = ccp(size.width - 206, size.height - 158);
    labelProtectDesc.color = ccc3(255, 255, 255);
    [self addChild:labelProtectDesc];
    
    
}

/**
 战报单击处理
 */
-(void) onPowerButton:(id)sender
{
    CCLOG(@"onPowerButton");
}

/**
 确认单击处理
 */
-(void) onSureButton:(id)sender
{
    CCLOG(@"onSureButton");
}

/**
 左边箭头 单击处理
 */
-(void) onLeftArrowButton:(id)sender
{
    CCLOG(@"左边箭头");
}

/**
 右边箭头 单击处理
 */
-(void) onRightArrowButton:(id)sender
{
    CCLOG(@"右边箭头");
}

/**
 删除
 */
-(void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
    //伤害列表
    if(self.hurtList)
    {
        [self.hurtList release];
    }
    //防御列表
    if(self.protectList)
    {
        [self.protectList release];
    }
    
    
    
    
}



@end
